<?php
namespace Admin\Controller;
use Tools\AdminController;
class UserController extends AdminController {
    function listtable(){//显示用户信息表格
	if(!empty($_POST)){
		var_dump($_POST);
	}else{
		$user=new \Model\UserModel();	
		//分页相关
		$total=$user->count();
		$per=2;
		//实例化分页类对象
		$page_obj=new \Tools\CssPage($total,$per);
		$sql="select * from user order by id asc ".$page_obj->limit;
		$info=$user->query($sql);
		$pagelist=$page_obj->fpage(array(3,4,5,6,7,8));
		$this->assign('total',$total);
		$this->assign('pagelist',$pagelist);	
		$this->assign('info',$info);
		$this->display();
	}
   }
	function listquery(){//查询用户信息
	if(!empty($_POST)){
		//var_dump($_POST);
		$user=new \Model\UserModel();	
		//根据查询条件连接查询语句
		if(isset($_POST['isexact'])){//精确查询
			$sql="select * from user where {$_POST['term']}='".$_POST['key']."' ";//条件查询
		}else{//模糊查询
			$sql="select * from user where {$_POST['term']} like '%".$_POST['key']."%' ";//条件查询
		}
		$sql.="and sex like '%".$_POST['sex']."%' ";//筛选性别
		$sql.="and status like '%".$_POST['status']."%' ";//筛选账号状态
		$sql.="order by ".$_POST['method']." ".$_POST['order'];//排序方式
		$sql.=" ".$page_obj->limit;//分页
		//echo $sql;
		$info=$user->query($sql);
		//echo count($info);
		//分页相关
		$total=count($info);
		$per=2;
		//实例化分页类对象
		$page_obj=new \Tools\CssPage($total,$per);
		$pagelist=$page_obj->fpage(array(3,4,5,6,7,8));
		$this->assign('total',$total);
		$this->assign('pagelist',$pagelist);	
		$this->assign('info',$info);
		$this->display('listtable');
	}else{
		$this->display();
	}
   }
	function edituser($id){
		if(!empty($_POST)){
			$user=new \Model\UserModel();
			//上传商品图片
				$cfg=array('rootPath'      => './upload/');
				$up=new \Think\Upload($cfg);
				if($_FILES['touxiang']['error']!=4){//判断是否上传了图片,不上传则不修改图片
					$upload_result=$up->uploadOne($_FILES['touxiang']);
					//图片路径
					$pic_path=$up->rootPath.$upload_result['savepath'].$upload_result['savename'];
					//修改为新的图片路径
					$_POST['touxiang']=ltrim($pic_path,'./');
				}
					if($user->save($_POST)){//成功
						$this->success('Index/success');
						$this->redirect('User/showlist',array(),3," ");
					}else{//失败
						$this->error('Index/error');
						$this->redirect('User/showlist',array(),3,' ');
					}
		}else{
			//获取该用户所有信息，填入表单
			$info=D('User')->find($id);
			$this->assign('info',$info);
			$this->display();
		}
	}
	function deleteuser($id){
		if(D('User')->delete($id)){
			$this->success('Index/success');
			$this->redirect('User/showlist',array(),3," ");
		}else{
			$this->error('Index/error');
			$this->redirect('User/showlist',array(),3," ");
		}
	}
	function changestatus($id){
		$user=new \Model\UserModel();	
		$status=D('User')->field('status')->find($id);
		//var_dump($status);
		if($status['status']==1){
			$sql="update user set status=0 where id=$id;";
			if($user->execute($sql)){
				echo "<i>解冻</i>";
			}
		}else{
			$sql="update user set status=1 where id=$id;";
			if($user->execute($sql)){
				echo "冻结";
			}
		}	
			
	}
	
}